/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TProductCatalog;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TCommodityDao;
import com.ruipengkj.service.TCommodityService;
/**
 * 说明：
 * 商品Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TCommodityServiceImpl implements TCommodityService{
	@Resource
	private TCommodityDao TCommodityDao;//DAO层
	//查询所有
	@Override
	public List<TCommodity> findService(TCommodity entity) throws Exception {
		String hql = "select o from TCommodity o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getTProductCatalog()!=null&&entity.getTProductCatalog().getCatalogID()!=null&&!entity.getTProductCatalog().getCatalogID().equals("")){
				whereHQL.append("TProductCatalog=?").append(BaseQuery.AND);
				param.add(entity.getTProductCatalog());
			}
			if(entity.getCommodityID()!=null&&!entity.getCommodityID().equals("")){
				whereHQL.append("commodityID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getCommodityID()+"%");
			}
			if(entity.getName()!=null&&!entity.getName().equals("")){
				whereHQL.append("name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getName()+"%");
			}
			if(entity.getBarcode()!=null&&!entity.getBarcode().equals("")){
				whereHQL.append("barcode").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBarcode()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TCommodityDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	
	//查询所有
	@Override
	public TCommodity findByIdService(String id) throws Exception {
		String hql = "select new TCommodity(o.commodityID, o.price, o.name,"+
			"o.discountPrice, o.batchPrice, o.barcode) from TCommodity o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if(id!=null&&!id.equals("")){
			whereHQL.append("o.commodityID=?");
			param.add(id);
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		List<TCommodity> tCommodityList = TCommodityDao.findDao(hql, whereHQL.toString(), param, orderby);
		return tCommodityList.size() == 0 ? null:tCommodityList.get(0);
	}

	
	//根据ID翻页查询
	@Override
	public PageModel<TCommodity> getScrollService(int firstResult,
			int maxResult, TCommodity entity) throws Exception {
		String hql = "select o from TCommodity o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getTProductCatalog()!=null&&entity.getTProductCatalog().getCatalogID()!=null&&!entity.getTProductCatalog().getCatalogID().equals("")){
				whereHQL.append("TProductCatalog=?").append(BaseQuery.AND);
				param.add(entity.getTProductCatalog());
			}
			if(entity.getCommodityID()!=null&&!entity.getCommodityID().equals("")){
				whereHQL.append("commodityID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getCommodityID()+"%");
			}
			if(entity.getName()!=null&&!entity.getName().equals("")){
				whereHQL.append("name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getName()+"%");
			}
			if(entity.getBarcode()!=null&&!entity.getBarcode().equals("")){
				whereHQL.append("barcode").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBarcode()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TCommodityDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	//多分类查询
	@Override
	public PageModel<TCommodity> getScrollService(int firstResult,
			int maxResult, TCommodity entity, List<TProductCatalog> tProductCatalogList) throws Exception {
		String hql = "select o from TCommodity o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		
		if (entity != null) {
			if (tProductCatalogList != null && tProductCatalogList.size() != 0) {
				for (int i = tProductCatalogList.size() - 1; i >= 0; --i) {
					TProductCatalog tProductCatalog = tProductCatalogList.get(i);
					if(tProductCatalog!=null&&tProductCatalog.getCatalogID()!=null&&!tProductCatalog.getCatalogID().equals("")){
						whereHQL.append("TProductCatalog=?").append(BaseQuery.OR);
						param.add(tProductCatalog);
					}
				}
				if(whereHQL.length()>4) {
					whereHQL.delete(whereHQL.length()-4, whereHQL.length());
//					whereHQL.append(")");
					whereHQL.append(BaseQuery.AND);
				}
			}
			
			if(entity.getCommodityID()!=null&&!entity.getCommodityID().equals("")){
				whereHQL.append("o.commodityID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getCommodityID()+"%");
			}
			if(entity.getName()!=null&&!entity.getName().equals("")){
				whereHQL.append("o.name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getName()+"%");
			}
			if(entity.getBarcode()!=null&&!entity.getBarcode().equals("")){
				whereHQL.append("o.barcode").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBarcode()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TCommodityDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	
	//修改
	@Override
	public void modifyService(TCommodity entity) throws Exception {
		TCommodityDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TCommodity entity) throws Exception {
		TCommodityDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TCommodityDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TCommodity getByIdService(String id) throws Exception {
		return TCommodityDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TCommodity entity) throws Exception {
		TCommodityDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TCommodity entity) throws Exception {
		return TCommodityDao.saveRetDao(entity);
	}
	
//	查询条形码最大的商品
	@Override
	public PageModel<TCommodity> getByMaxBarcodeService() throws Exception {
		String hql = "select new TCommodity(o.commodityID, o.price, o.name,"
			+"o.discountPrice, o.batchPrice, o.barcode) from TCommodity o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("barcode", BaseQuery.DESC);
		return TCommodityDao.getScrollDao(hql, whereHQL.toString(), param, 0, 1, orderby);
	}
	
//	根据条形码查询 查询条形码是否为空
	@Override
	public PageModel<TCommodity> getByBarcodeNullService(int firstResult,
			int maxResult,int Barcode) throws Exception {
		String hql = "select o from TCommodity o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (Barcode == 1) {
			whereHQL.append("barcode=?").append(BaseQuery.OR);
			param.add("");
			whereHQL.append("barcode=?");
			param.add(null);
		}
		if (Barcode == 2) {
			whereHQL.append("barcode<>?");
			param.add("");
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("barcode", BaseQuery.DESC);
		return TCommodityDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	//根据条形码查询
	@Override
	public TCommodity getByBarcodeService(String Barcode) throws Exception {
		if(Barcode!=null&&!Barcode.equals("")){
			String hql = "select o from TCommodity o";
			StringBuffer whereHQL = new StringBuffer();
			List<Object> param = new ArrayList<Object>();
			whereHQL.append("Barcode=?");
			param.add(Barcode);
			List<TCommodity> list=TCommodityDao.findDao(hql, whereHQL.toString(), param, null);
			if(list!=null&&list.size()!=0){
				return list.get(0);
			}
		}
		return null;
	}
	public TCommodityDao getTCommodityDao() {
		return TCommodityDao;
	}

	public void setTCommodityDao(TCommodityDao TCommodityDao) {
		this.TCommodityDao = TCommodityDao;
	}


	@Override
	public List<TCommodity> findTCommodityExportService(TCommodity entity,List<TProductCatalog> tProductCatalogList) throws Exception {
		String hql = "select o from TCommodity o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (tProductCatalogList != null && tProductCatalogList.size() != 0) {
//				if (tProductCatalogList.get(0) != null && tProductCatalogList.get(0).getCatalogID() != null && !tProductCatalogList.get(0).getCatalogID().equals(""))
//					whereHQL.append("(");
				for (int i = tProductCatalogList.size() - 1; i >= 0; --i) {
					TProductCatalog tProductCatalog = tProductCatalogList.get(i);
					if(tProductCatalog!=null&&tProductCatalog.getCatalogID()!=null&&!tProductCatalog.getCatalogID().equals("")){
						whereHQL.append("TProductCatalog=?").append(BaseQuery.OR);
						param.add(tProductCatalog);
					}
				}
				if(whereHQL.length()>4) {
					whereHQL.delete(whereHQL.length()-4, whereHQL.length());
//					whereHQL.append(")");
					whereHQL.append(BaseQuery.AND);
				}
			}
			
			if(entity.getCommodityID()!=null&&!entity.getCommodityID().equals("")){
				whereHQL.append("o.commodityID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getCommodityID()+"%");
			}
			if(entity.getName()!=null&&!entity.getName().equals("")){
				whereHQL.append("o.name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getName()+"%");
			}
			if(entity.getBarcode()!=null&&!entity.getBarcode().equals("")){
				whereHQL.append("o.barcode").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBarcode()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TCommodityDao.findDao(hql, whereHQL.toString(), param, orderby);
	}


	@Override
	public PageModel<TCommodity> getScrollListService(int firstResult,
			int maxResult, TCommodity entity, List<TProductCatalog> tProductCatalogList) throws Exception {
		
		String hql = "select new TCommodity(o.commodityID, o.price, o.des, o.name," +
			"o.point, o.disCount, o.discountPrice, o.score,"+
			"o.detail, o.type, o.status, o.clickCount, o.createTime,"+
			"o.updateTime, o.saleCount, o.commentCount, o.scoreCount,"+
			"o.barcode, o.batchPrice, o.unit, o.memCode,"+
			"o.std, o.madePlace, o.color, o.batch,"+
			"o.validity, o.validityUnit, o.avgPrice) from TCommodity o left join o.TProductCatalog p";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		
		if (entity != null) {
			if (tProductCatalogList != null && tProductCatalogList.size() != 0) {
				for (int i = tProductCatalogList.size() - 1; i >= 0; --i) {
					TProductCatalog tProductCatalog = tProductCatalogList.get(i);
					if(tProductCatalog!=null&&tProductCatalog.getCatalogID()!=null&&!tProductCatalog.getCatalogID().equals("")){
						whereHQL.append("o.TProductCatalog=?").append(BaseQuery.OR);
						param.add(tProductCatalog);
					}
				}
				
				if(whereHQL.length()>4) {
					whereHQL.delete(whereHQL.length()-4, whereHQL.length());
					whereHQL.append(BaseQuery.AND);
				}
			}
			
			if(entity.getCommodityID()!=null&&!entity.getCommodityID().equals("")){
				whereHQL.append("o.commodityID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getCommodityID()+"%");
			}
			if(entity.getName()!=null&&!entity.getName().equals("")){
				whereHQL.append("o.name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getName()+"%");
			}
			if(entity.getBarcode()!=null&&!entity.getBarcode().equals("")){
				whereHQL.append("o.barcode").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBarcode()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("TProductCatalog.priority", BaseQuery.ASC);
		
		return TCommodityDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	
	/**翻页查询商品列表+进销属性查询，isSales=true，销售的商品，false：仅进货的商品*/
	@Override
	public PageModel<TCommodity> getScrollListService(int firstResult,
			int maxResult, TCommodity entity) throws Exception {
		
		String hql = "select new TCommodity(o.commodityID, o.price, o.des, o.name," +
			"o.point, o.disCount, o.discountPrice, o.score,"+
			"o.detail, o.type, o.status, o.clickCount, o.createTime,"+
			"o.updateTime, o.saleCount, o.commentCount, o.scoreCount,"+
			"o.barcode, o.batchPrice, o.unit, o.memCode,"+
			"o.std, o.madePlace, o.color, o.batch,"+
			"o.validity, o.validityUnit, o.avgPrice) from TCommodity o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			
			if(entity.getCommodityID()!=null&&!entity.getCommodityID().equals("")){
				whereHQL.append("commodityID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getCommodityID()+"%");
			}
			if(entity.getName()!=null&&!entity.getName().equals("")){
				whereHQL.append("name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getName()+"%");
			}
			if(entity.getBarcode()!=null&&!entity.getBarcode().equals("")){
				whereHQL.append("barcode").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBarcode()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TCommodityDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	
	/**根据商品分类ID优化查询*/
	@Override
	public PageModel<TCommodity> getScrollService(int firstResult,
			int maxResult, String CatalogId) throws Exception {
		String hql = "select new TCommodity(o.commodityID,  o.name," +
			"o.discountPrice, o.disCount, o.unit) from TCommodity o";
		
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
			
		if (CatalogId != null && CatalogId.length() != 0) {
			whereHQL.append("o.TProductCatalog=?");
			param.add(new TProductCatalog(CatalogId));
		}
		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TCommodityDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	
	/**
	 * select o.commodityID, o.price, o.name, o.discountPrice,
	 * o.barcode, o.batchPrice, o.unit from ruipeng_tb_TCommodity o 
	 * left join ruipeng_tb_TProductCatalog p on o.TProductCatalogid = p.catalogID  
	 * where (o.intoSales='仅销售' or o.intoSales='进货销售' or (o.intoSales is null and 
	 * (p.intoSales is null or p.intoSales!='仅进货'))) and p.catalogid='aAAh2b8py6bMXss' limit 15;
	 * 
	 * **/
	@Override
	public int executeSQL(String sql) {
		return TCommodityDao.executeSQL(sql);
	}

	@Override
	public List<Object[]> querySQL(String sql) {
		return TCommodityDao.querySQL(sql);
	}

	@Override
	public List<TCommodity> findByNameService(String name) throws Exception {
		String hql = "select new TCommodity(o.commodityID) from TCommodity o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if(name!=null&&!name.equals("")){
			whereHQL.append("o.name=?");
			param.add(name);
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TCommodityDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
}